// pages/admin/login.js
Page({
  data: {
    list: [],
    isLogin: false, // 是否登陆成功  
    username: '', // 用户名
    password: '', // 密码
    loading: false, // 登录按钮加载状态
    errorMsg: '' // 错误提示信息
  },
  onLoad() {
    const isLogin = wx.getStorageSync('isLogin')
    if (isLogin) {
      this.setData({
        isLogin: true
      })
      this.getList()
    }
  },
  onPullDownRefresh() {
    if (this.data.isLogin) {
      this.getList()
    } else {
      wx.stopPullDownRefresh()
    }
  },
  hanldePreView(e) {
    const imgPath = e.currentTarget.dataset.img
    wx.previewImage({
      current: 'imgPath', // 当前显示图片的http链接
      urls: [imgPath] // 需要预览的图片http链接列表
    })
  },
  formatDate(d) {
    const date = new Date(d)
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    const hours = String(date.getHours()).padStart(2, '0')
    const minutes = String(date.getMinutes()).padStart(2, '0')
    const seconds = String(date.getSeconds()).padStart(2, '0')

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  },
  getList() {
    let _this = this
    wx.cloud.callFunction({
      name: 'userSubscribeFun',
      data: {
        type: 'getAllSubScribeList',
      },
      complete: res => {
        console.log(res, '获取列表---------')
        wx.stopPullDownRefresh()
        if (res.result.code == 200) {
          _this.setData({
            list: res.result.data.map(d => {
              return {
                ...d,
                create_time: _this.formatDate(d.create_time)
              }
            })
          })
          return
        }
        wx.showToast({
          title: '获取预约列表失败',
          icon: 'none'
        })
      }
    })
  },

  // 处理打电话按钮点击
  makePhoneCall: function (e) {
    const item = e.currentTarget.dataset.item;
    wx.makePhoneCall({
      phoneNumber: item.phone, // 替换为实际电话号码
      success: function () {
        console.log('拨打电话成功');
      },
      fail: function (err) {
        console.error('拨打电话失败', err);
        wx.showToast({
          title: '拨打电话失败',
          icon: 'none'
        });
      }
    });
  },
  handleCheckMakePhone(e) {
    const item = e.currentTarget.dataset.item;
    const _this = this
    wx.showModal({
      title: '提示',
      content: '是否修改状态为已叫号？',
      complete: (res) => {
        if (res.confirm) {
          _this.switchStatus(item)
        }
      }
    })
  },
  switchStatus(item) {
    const _this = this
    wx.cloud.callFunction({
      name: 'userSubscribeFun',
      data: {
        type: 'updateStatus',
        params: {
          id: item._id
        }
      },
      complete: res => {
        if (res.result.code == 200) {
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
          _this.getList()
          return
        }
        wx.showToast({
          title: '修改状态失败',
          icon: 'none'
        })
      }
    })
  },
  // 用户名输入
  onUsernameInput(e) {
    this.setData({
      username: e.detail.value,
      errorMsg: '' // 清空错误信息
    });
  },

  // 密码输入
  onPasswordInput(e) {
    this.setData({
      password: e.detail.value,
      errorMsg: '' // 清空错误信息
    });
  },

  // 登录处理
  handleLogin() {
    const {
      username,
      password
    } = this.data;

    // 简单验证
    if (!username.trim()) {
      this.setData({
        errorMsg: '请输入账号'
      });
      return;
    }

    if (!password.trim()) {
      this.setData({
        errorMsg: '请输入密码'
      });
      return;
    }

    // 显示加载状态
    this.setData({
      loading: true,
      errorMsg: ''
    });
    // 模拟登录成功/失败
    if (username === 'admin' && password === '123456') {
      wx.showToast({
        title: '登录成功',
        icon: 'success'
      });
      this.setData({
        isLogin: true
      })
      wx.setStorageSync('isLogin', true)
      this.getList()
    } else {
      this.setData({
        errorMsg: '账号或密码错误',
        loading: false
      });
    }
  }
})